﻿//
// WebTools Library by S. Ali Tokmen
// http://code.google.com/p/web-tools/
// http://ali.tokmen.com/
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// 1. The above copyright notice and this permission notice shall be
//    included in all copies or substantial portions of the Software.
// 2. Namespaces or Class names of the Software components shall not
//    be modified.
// 3. Neither the name of the copyright holders nor the names of its
//    contributors may be used to endorse or promote products derived from
//    this Software without specific prior written permission.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using System;

using HTML;

namespace GoogleCode.WebTools.Examples
{
    class Google
    {
        static public void Execute()
        {
            HTTPWorker worker = new HTTPWorker();

            // Fetch the Google home page
            worker.SetTarget("http://www.google.com/", HTTPRequestType.Get);

            // HTTPWorker.ResponseText will automatically
            // send the HTTP request and save the reponse if necessary
            String googleHome = worker.ResponseText;
            Console.WriteLine(googleHome);

            Console.WriteLine("Press ENTER to continue...");
            Console.ReadLine();

            // Let's now add some arguments to our request
            // Google uses the argument "q" for the search query
            worker.SetTarget("http://www.google.com/search", HTTPRequestType.Get);
            worker.AddValue("q", "ali tokmen");

            // HTTPWorker.ResponseText will automatically
            // send the HTTP request and save the reponse if necessary
            String googleSearch = worker.ResponseText;
            Console.WriteLine(googleSearch);

            Console.WriteLine("Press ENTER to continue...");
            Console.ReadLine();

            ParseHTML parser = new ParseHTML();

            // Give the HTML file to read
            parser.Source = googleSearch;

            // The ParseHTML class is an SAX parser. This means it will go
            // deeper in the document as you call ParseHTML.Parse()
            while (!parser.Eof())
            {
                // ParseHTML.Parse() returns the current character that's been
                // parsed. It returns 0 if the parsed character is an HTML tag.
                if (parser.Parse() == 0)
                {
                    AttributeList tag = parser.GetTag();

                    if (tag.Name == "a")
                    {
                        // We found a link! Google will add as class name "l"
                        // for search results, as a result if a link's class
                        // name is "l" it should be a search result.
                        if (tag["class"] != null && tag["class"].Value == "l")
                        {
                            // This is a search result

                            // ParseHTML.GetTag gets the content of the current
                            // HTML tag
                            String linkContent = ParseHTML.GetTag(parser, tag);

                            // The "a" tag's href element gives the target URL
                            Console.WriteLine(tag["href"].Value + ": " + linkContent);
                        }
                    }
                }
            }

            Console.WriteLine("Press ENTER to continue...");
            Console.ReadLine();
        }
    }
}
